/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.acme.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author iiping
 */
public class Tbl1Operations extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        DAOCentral ds = DAOCentral.getInstance();
        HashSet<TOTable1> results  ;
        response.setContentType("text/plain;utf-8");
        PrintWriter out = response.getWriter();

      try{
        int route = 0;
        HashSet<Integer> keys = new HashSet<Integer>();

        //table1/getresults
        //test if key or keys
        if(request.getParameter("key") != null){ // 2

          //now evaluate if key is ok
          String strK = request.getParameter("key");
          try{
            int tmpK = Integer.parseInt(strK);
            
            if(strK != null && tmpK > 0){
              keys.add(Integer.valueOf(tmpK));
              route = 2;
            }
          }
          catch(RuntimeException e){
            route  = 0;
          }


          
        } else if(request.getParameter("keys") != null){ // 1
          String strks = request.getParameter("keys");
          String[] strK = null;
          //bug remover
          
          try{
            strks = URLEncoder.encode(strks, "UTF-8");
            strK = strks.split("\\+");

                  //lets just check if all are valid integers and is greater than 0
                  //since we do this we will just add it directly to keys
                  for(int i=0; strK.length > i ; i++){
                    try{
                      int kVal = Integer.parseInt(strK[i]);
                      keys.add(Integer.valueOf(kVal));
                    }
                    catch(RuntimeException e){
                      
                      route  = 0;
                    }
                    route = 2;
                  }
                  route =1 ;
          }
          catch(UnsupportedEncodingException ex){
            route  = 0;
          }


        } else{ // 0
         //and do nothing
        }
        switch(route){
          case 2:
          case 1:
            results = ds.retTOTable1(((Integer[]) keys.toArray(new Integer[0])) );
            break;
          case 0:
          default:
            results  = new HashSet<TOTable1>();
            break;
        }
        //just flow
        out.write("{"); // begin of dataset
        int c = 0;
        for(TOTable1 result : results){
          if(c > 0)
            out.write(",");
          out.write(result.toString());
          c++;
        }
        out.write("}"); //end of dataset
      } finally{
        out.close();
      }

    }




    




    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
